package com.yycx.module.bus.provider.handler.haiying.order.tuihuo;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.interceptor.SaveInterceptor;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.hy.HyOrderDetail;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 海鹰退货回款保存扩展器
 *
 * @author zyf
 */
@Component("hyTuiHuoPaymentSaveHandler")
public class HyTuiHuoPaymentSaveHandler implements SaveInterceptor<BusPayment> {

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private HyOrderService orderService;

    @Autowired
    private HyOrderDetailService hyOrderDetailService;


    @Override
    public void prepare(CriteriaSave cs, EntityMap entityMap, BusPayment payment) {
        String orderDetailsId=payment.getOrderDetailsId();
        BusPayment check = paymentService.getByOrderDetailsIdAndAmountType(orderDetailsId, AmountTypeEnum.CustomRefund);
        if (FlymeUtils.isNotEmpty(check)) {
            ApiAssert.failure("请不要重复回款");
        }
        HyOrderDetail orderDetails=hyOrderDetailService.getById(orderDetailsId);
        HyOrder order=orderService.getById(orderDetails.getOrderId());
        payment.setOrganizationId(orderDetails.getOrganizationId());
        payment.setCompanyId(orderDetails.getCompanyId());
        payment.setDeptId(orderDetails.getDeptId());
        payment.setTitle("客户退货回款");
        payment.setAmount(orderDetails.getSaleAmount());
        payment.setOrderId(orderDetails.getOrderId());
        payment.setPayDate(payment.getAmountDate());
        payment.setOrderDetailsId(orderDetails.getOrderDetailsId());
        payment.setCustomId(orderDetails.getCustomCode());
        payment.setAmountType(AmountTypeEnum.CustomRefund.getCode());
        String orderNo = order.getOrderNo();
        payment.setPaymentNo("H" + orderNo);
        payment.setPrintTag(CommonConstants.DISABLED);
        payment.setUserId(OpenHelper.getUserId());
        payment.setNickName(OpenHelper.getNickName());
        payment.setPaymentState(CommonConstants.INT_0);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusPayment entity) {
        String orderDetailsId = entity.getOrderDetailsId();
        //管理回款ID
        //hyOrderDetailService.updateHuiKuan(entity,orderDetailsId);
    }

}
